package cn.hackcloud.mq;

import java.util.List;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
	public static void main(String[] args) throws Exception {
		DefaultMQProducer producer = new DefaultMQProducer("hackcloud");
		producer.setNamesrvAddr("47.93.251.127:9876");
		producer.setVipChannelEnabled(false);
		producer.start();
		for (int i = 1; i < 3; i++) {
			Message message = new Message("topicTest", "TagA", "消息" + i,
					("hello laijun > " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
			SendResult sendResult = producer.send(message, new MessageQueueSelector() {

				@Override
				public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
					System.out.println(mqs.size());
					System.out.println(mqs.toString());
					for (MessageQueue messageQueue : mqs) {
						System.out.println(messageQueue.toString());
					}
					int id = (Integer) arg;
					int index = id % mqs.size();
					System.out.println("index >" + index);
					return mqs.get(index);
				}
			}, i);
			System.err.println(sendResult);
		}

	}
}
